package com.fhzn.auth.tools;

import com.fhzn.auth.entity.Role;
import com.fhzn.auth.enums.AdminSignEnum;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @author fhzn
 */
public class AuthUtil {

    public static boolean isSuperAdmin(List<Role> roleList) {
        if (CollectionUtils.isEmpty(roleList)) {
            return false;
        }
        for (Role r : roleList) {
            if (isSuperAdmin(r)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isAdmin(List<Role> roleList) {
        if (CollectionUtils.isEmpty(roleList)) {
            return false;
        }
        for (Role r : roleList) {
            if (isAdmin(r)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isAdmin(List<Role> roleList, String appCode) {
        if (CollectionUtils.isEmpty(roleList)) {
            return false;
        }
        for (Role r : roleList) {
            if (isAdmin(r, appCode)) {
                return true;
            }
        }
        return false;
    }


    public static boolean isSuperAdmin(Role role) {
        return StringUtils.equals(role.getSign(), AdminSignEnum.SUPERADMIN.getSign());
    }

    public static boolean isAdmin(Role role) {
        return StringUtils.equals(role.getSign(), AdminSignEnum.ADMIN.getSign());
    }

    public static boolean isSubAdmin(Role role) {
        return StringUtils.equals(role.getSign(), AdminSignEnum.SUB_ADMIN.getSign());
    }

    public static boolean isSubAdmin(List<Role> roles) {
        for (Role r : roles) {
            if (isSubAdmin(r)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isAdminOrSuperAdmin(Role role) {
        return StringUtils.equals(role.getSign(), AdminSignEnum.ADMIN.getSign())
                || StringUtils.equals(role.getSign(), AdminSignEnum.SUPERADMIN.getSign());
    }

    /**
     * 判断是否为管理员,超级管理员，子管理员其中之一
     *
     * @param role 角色信息
     * @return true/false
     */
    public static boolean isAdminOrSuperAdminOrSubAdmin(Role role) {
        return StringUtils.equals(role.getSign(), AdminSignEnum.ADMIN.getSign())
                || StringUtils.equals(role.getSign(), AdminSignEnum.SUPERADMIN.getSign())
                || StringUtils.equals(role.getSign(), AdminSignEnum.SUB_ADMIN.getSign());
    }


    public static boolean isAdminOrSuperAdmin(List<Role> roles) {
        for (Role r : roles) {
            if (isAdminOrSuperAdmin(r)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isAdminOrSuperAdmin(List<Role> roles, String appCode) {
        return isSuperAdmin(roles) || isAdmin(roles, appCode);
    }

    public static List<Role> filterAdminRoles(List<Role> roleList) {
        if (CollectionUtils.isEmpty(roleList)) {
            return Lists.newArrayList();
        }
        List<Role> result = Lists.newArrayList();
        for (Role role : roleList) {
            if (isAdmin(role)) {
                result.add(role);
            }
        }
        return result;
    }

    public static List<Role> filterOrdinaryRoles(List<Role> roleList) {
        if (CollectionUtils.isEmpty(roleList)) {
            return Lists.newArrayList();
        }
        List<Role> result = Lists.newArrayList();
        for (Role role : roleList) {
            if (!isAdmin(role) && !isSuperAdmin(role)) {
                result.add(role);
            }
        }
        return result;
    }

    /**
     * 判断角色列表是是否含有指定角色
     *
     * @param roleList 角色信息列表
     * @param roleCode 角色code
     * @return true/false
     */
    public static boolean hasRole(List<Role> roleList, String roleCode) {
        if (CollectionUtils.isEmpty(roleList)) {
            return false;
        }
        for (Role role : roleList) {
            if (StringUtils.equals(role.getCode(), roleCode)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否是某系统的管理员
     *
     * @param role    角色信息
     * @param appCode 应用code
     * @return true/false
     */
    public static boolean isAdmin(Role role, String appCode) {
        return (StringUtils.equals(role.getSign(), AdminSignEnum.ADMIN.getSign())
                || StringUtils.equals(role.getSign(), AdminSignEnum.SUPERADMIN.getSign()))
                && StringUtils.equals(role.getAppCode(), appCode);
    }

    /**
     * 判断是否操作角色
     *
     * @param roleList 角色信息列表
     * @return true/false
     */
    public static boolean canOperateRole(List<Role> roleList) {
        if (CollectionUtils.isEmpty(roleList)) {
            return false;
        }
        for (Role role : roleList) {
            if (isAdminOrSuperAdminOrSubAdmin(role)) {
                return true;
            }
        }
        return false;
    }


}
